GBML manual / Instances

x

This instance’s current horizontal position, in pixels.

Syntax

x: real

Description

This instance's current horizontal position in the room, in pixels. Reading it never lies about where an instance actually is on screen this frame; writing it teleports the instance there at once; nothing eases or animates a change to it on its own.

It counts from the room's top-left corner: x = 0 is the room's left edge, and x gets bigger as you go right. In a room 320 pixels wide the right edge is at x = 320 and the middle is at x = 160. See Where (0, 0) is.

An instance's x/y together are where its origin sits. The origin is the middle of its sprite unless you chose another spot for it in the object editor, so by default it is the center of the picture, not a corner - worth knowing the first time a placed sprite looks shifted from where you expected. (That is a different question from where the room's own (0, 0) is. The room is counted from its top-left corner, as above.)

Example

if (btn_held(BTN_RIGHT)) {
    x += 2;
}

Moves the instance 2 pixels right every frame BTN_RIGHT is held, by writing x directly rather than going through speed/direction.

See also: yObjects and instancesWhere the machine shows through